*Robustness checks
*Set CD to working directory
******************3.3.1 Extensive Margin****************************

clear all

set maxvar 100000

use "trade_counts.dta", clear
duplicates drop year importer exporter, force
decode importer, gen(importer_iso3)
decode exporter, gen (exporter_iso3)

generate any_digital = 1 if free_data_flow==1 | tech_neutrality==1 | ec_duty_prohibit==1 | electronic_sig==1 | data_protection==1 | cybersecurity==1 | goods_big_data==1
replace any_digital = 0 if any_digital==.
generate provision_count=free_data_flow+tech_neutrality+ec_duty_prohibit+electronic_sig+data_protection+cybersecurity+goods_big_data
replace provision_count=0 if provision_count==.
generate provision_index = provision_count/7
replace minimum_bandwidth=minimum_bandwidth/1000
generate colony_ever = colony_of_destination_ever+colony_of_origin_ever
generate log_distance=log(distance)

egen max_bandwidth=max(minimum_bandwidth)
generate bandwidth_index = minimum_bandwidth/max_bandwidth

decode importer, generate(imp_iso3)
decode exporter, generate(exp_iso3)

*Generate variable that indicates high income importer (both high and upper middle income)
generate high_income_exp=0

replace high_income_exp=1 if exp_iso3=="AND" | exp_iso3=="ATG" | exp_iso3=="ABW" | exp_iso3=="AUS" | exp_iso3=="AUT" | exp_iso3=="BHS" | exp_iso3=="BHR" | exp_iso3=="BRB" | exp_iso3=="BEL" | exp_iso3=="BMU" | exp_iso3=="VGB" | exp_iso3=="BRN" | exp_iso3=="CAN" | exp_iso3=="CYM" | exp_iso3=="CHL" | exp_iso3=="HRV" | exp_iso3=="CUW" | exp_iso3=="CYP" | exp_iso3=="CZE" | exp_iso3=="DNK" | exp_iso3=="EST" | exp_iso3=="FRO" | exp_iso3=="FIN" | exp_iso3=="FRA" | exp_iso3=="PYF" | exp_iso3=="DEU" | exp_iso3=="GIB" | exp_iso3=="GRC" | exp_iso3=="GRL" | exp_iso3=="GUM" | exp_iso3=="HKG" | exp_iso3=="HUN" | exp_iso3=="ISL" | exp_iso3=="IRL" | exp_iso3=="IMN" | exp_iso3=="ISR" | exp_iso3=="ITA" | exp_iso3=="JPN" | exp_iso3=="KOR" | exp_iso3=="KWT" | exp_iso3=="LVA" | exp_iso3=="LIE" | exp_iso3=="LTU" | exp_iso3=="LUX" | exp_iso3=="MAC" | exp_iso3=="MLT" | exp_iso3=="MCO" | exp_iso3=="NRU" | exp_iso3=="NLD" | exp_iso3=="NCL" | exp_iso3=="NZL" | exp_iso3=="MNP" | exp_iso3=="NOR" | exp_iso3=="OMN" | exp_iso3=="PLW" | exp_iso3=="POL" | exp_iso3=="PRT" | exp_iso3=="PRI" | exp_iso3=="QAT" | exp_iso3=="SMR" | exp_iso3=="SAU" | exp_iso3=="SYC" | exp_iso3=="SGP" | exp_iso3=="SXM" | exp_iso3=="SVK" | exp_iso3=="SVN" | exp_iso3=="ESP" | exp_iso3=="KNA" | exp_iso3=="MAF" | exp_iso3=="SWE" | exp_iso3=="CHE" | exp_iso3=="TWN" | exp_iso3=="TTO" | exp_iso3=="TCA" | exp_iso3=="ARE" | exp_iso3=="GBR" | exp_iso3=="USA" | exp_iso3=="URY" | exp_iso3=="VIR"  | exp_iso3=="ROU" | exp_iso3=="PAN"
*upper middle income
replace high_income_exp=1 if exp_iso3=="ALB" | exp_iso3=="FJI" | exp_iso3=="NAM" | exp_iso3=="ASM" | exp_iso3=="GAB" | exp_iso3=="MKD" | exp_iso3=="ARG" | exp_iso3=="GEO" | exp_iso3=="PLW" | exp_iso3=="ARM" | exp_iso3=="GRD" | exp_iso3=="PRY" | exp_iso3=="AZE" | exp_iso3=="GTM" | exp_iso3=="PER" | exp_iso3=="BLR" | exp_iso3=="GUY" | exp_iso3=="RUS" | exp_iso3=="BLZ" | exp_iso3=="IRQ" | exp_iso3=="SRB" | exp_iso3=="BIH" | exp_iso3=="JAM" | exp_iso3=="ZAF" | exp_iso3=="BWA" | exp_iso3=="JOR" | exp_iso3=="LCA" | exp_iso3=="BRA" | exp_iso3=="KAZ" | exp_iso3=="VCT" | exp_iso3=="BLG" | exp_iso3=="KSV" | exp_iso3=="SUR" | exp_iso3=="CHN" | exp_iso3=="LBY" | exp_iso3=="THA" | exp_iso3=="COL" | exp_iso3=="MYS" | exp_iso3=="TON" | exp_iso3=="CRI" | exp_iso3=="MDV" | exp_iso3=="TUR" | exp_iso3=="CUB" | exp_iso3=="MHL" | exp_iso3=="TKM" | exp_iso3=="DMA" | exp_iso3=="MUS" | exp_iso3=="TUV" | exp_iso3=="DOM" | exp_iso3=="MEX" | exp_iso3=="GNQ" | exp_iso3=="MDA" | exp_iso3=="ECU" | exp_iso3=="MNE"  


generate low_inc_exp=0
replace low_inc_exp=1 if high_income_exp==0

gen high_int_ex=high_income_exp*internet_use_pair
gen low_int_ex = low_inc_exp*internet_use_pair

foreach v of var tech_neutrality- free_data_flow{
	replace `v' = 1 if `v'>0
}

gen high_digital_ex=high_income_exp*provision_index
gen low_digital_ex=low_inc_exp*provision_index
gen high_any_dig_ex=high_income_exp*any_digital
gen low_any_dig_ex=low_inc_exp*any_digital

eststo clear
eststo: flex number_of_products agree_pta member_eu_joint high_int_ex low_int_ex high_any_dig_ex low_any_dig_ex  bandwidth_index log_distance contiguity common_language colony_ever i.importer#i.year i.exporter#i.year, ul(170) 

esttab using "all_flex_results.csv", star(* 0.10 ** 0.05 *** 0.01) se r2 br aic  replace

use "trade_counts.dta", clear

decode importer, gen(importer_iso3)
decode exporter, gen (exporter_iso3)

generate any_digital = 1 if free_data_flow==1 | tech_neutrality==1 | ec_duty_prohibit==1 | electronic_sig==1 | data_protection==1 | cybersecurity==1 | goods_big_data==1
replace any_digital = 0 if any_digital==.
generate provision_count=free_data_flow+tech_neutrality+ec_duty_prohibit+electronic_sig+data_protection+cybersecurity+goods_big_data
replace provision_count=0 if provision_count==.
generate provision_index = provision_count/7
replace minimum_bandwidth=minimum_bandwidth/1000
generate colony_ever = colony_of_destination_ever+colony_of_origin_ever
generate log_distance=log(distance)

egen max_bandwidth=max(minimum_bandwidth)
generate bandwidth_index = minimum_bandwidth/max_bandwidth

decode importer, generate(imp_iso3)
decode exporter, generate(exp_iso3)

generate high_income_exp=0

replace high_income_exp=1 if exp_iso3=="AND" | exp_iso3=="ATG" | exp_iso3=="ABW" | exp_iso3=="AUS" | exp_iso3=="AUT" | exp_iso3=="BHS" | exp_iso3=="BHR" | exp_iso3=="BRB" | exp_iso3=="BEL" | exp_iso3=="BMU" | exp_iso3=="VGB" | exp_iso3=="BRN" | exp_iso3=="CAN" | exp_iso3=="CYM" | exp_iso3=="CHL" | exp_iso3=="HRV" | exp_iso3=="CUW" | exp_iso3=="CYP" | exp_iso3=="CZE" | exp_iso3=="DNK" | exp_iso3=="EST" | exp_iso3=="FRO" | exp_iso3=="FIN" | exp_iso3=="FRA" | exp_iso3=="PYF" | exp_iso3=="DEU" | exp_iso3=="GIB" | exp_iso3=="GRC" | exp_iso3=="GRL" | exp_iso3=="GUM" | exp_iso3=="HKG" | exp_iso3=="HUN" | exp_iso3=="ISL" | exp_iso3=="IRL" | exp_iso3=="IMN" | exp_iso3=="ISR" | exp_iso3=="ITA" | exp_iso3=="JPN" | exp_iso3=="KOR" | exp_iso3=="KWT" | exp_iso3=="LVA" | exp_iso3=="LIE" | exp_iso3=="LTU" | exp_iso3=="LUX" | exp_iso3=="MAC" | exp_iso3=="MLT" | exp_iso3=="MCO" | exp_iso3=="NRU" | exp_iso3=="NLD" | exp_iso3=="NCL" | exp_iso3=="NZL" | exp_iso3=="MNP" | exp_iso3=="NOR" | exp_iso3=="OMN" | exp_iso3=="PLW" | exp_iso3=="POL" | exp_iso3=="PRT" | exp_iso3=="PRI" | exp_iso3=="QAT" | exp_iso3=="SMR" | exp_iso3=="SAU" | exp_iso3=="SYC" | exp_iso3=="SGP" | exp_iso3=="SXM" | exp_iso3=="SVK" | exp_iso3=="SVN" | exp_iso3=="ESP" | exp_iso3=="KNA" | exp_iso3=="MAF" | exp_iso3=="SWE" | exp_iso3=="CHE" | exp_iso3=="TWN" | exp_iso3=="TTO" | exp_iso3=="TCA" | exp_iso3=="ARE" | exp_iso3=="GBR" | exp_iso3=="USA" | exp_iso3=="URY" | exp_iso3=="VIR"  | exp_iso3=="ROU" | exp_iso3=="PAN" 
*upper middle income
replace high_income_exp=1 if exp_iso3=="ALB" | exp_iso3=="FJI" | exp_iso3=="NAM" | exp_iso3=="ASM" | exp_iso3=="GAB" | exp_iso3=="MKD" | exp_iso3=="ARG" | exp_iso3=="GEO" | exp_iso3=="PLW" | exp_iso3=="ARM" | exp_iso3=="GRD" | exp_iso3=="PRY" | exp_iso3=="AZE" | exp_iso3=="GTM" | exp_iso3=="PER" | exp_iso3=="BLR" | exp_iso3=="GUY" | exp_iso3=="RUS" | exp_iso3=="BLZ" | exp_iso3=="IRQ" | exp_iso3=="SRB" | exp_iso3=="BIH" | exp_iso3=="JAM" | exp_iso3=="ZAF" | exp_iso3=="BWA" | exp_iso3=="JOR" | exp_iso3=="LCA" | exp_iso3=="BRA" | exp_iso3=="KAZ" | exp_iso3=="VCT" | exp_iso3=="BLG" | exp_iso3=="KSV" | exp_iso3=="SUR" | exp_iso3=="CHN" | exp_iso3=="LBY" | exp_iso3=="THA" | exp_iso3=="COL" | exp_iso3=="MYS" | exp_iso3=="TON" | exp_iso3=="CRI" | exp_iso3=="MDV" | exp_iso3=="TUR" | exp_iso3=="CUB" | exp_iso3=="MHL" | exp_iso3=="TKM" | exp_iso3=="DMA" | exp_iso3=="MUS" | exp_iso3=="TUV" | exp_iso3=="DOM" | exp_iso3=="MEX" | exp_iso3=="GNQ" | exp_iso3=="MDA" | exp_iso3=="ECU" | exp_iso3=="MNE"  


generate low_inc_exp=0
replace low_inc_exp=1 if high_income_exp==0

gen high_int_ex=high_income_exp*internet_use_pair
gen low_int_ex = low_inc_exp*internet_use_pair

foreach v of var tech_neutrality- free_data_flow{
	replace `v' = 1 if `v'>0
}

gen high_digital_ex=high_income_exp*provision_index
gen low_digital_ex=low_inc_exp*provision_index
gen high_any_dig_ex=high_income_exp*any_digital
gen low_any_dig_ex=low_inc_exp*any_digital


eststo: flex products_bytype agree_pta member_eu_joint high_int_ex low_int_ex high_any_dig_ex low_any_dig_ex  bandwidth_index log_distance contiguity common_language colony_ever  i.importer#i.year i.exporter#i.year if goods_svcs=="goods", ul(153) 
esttab using "flex_goods_results.csv", star(* 0.10 ** 0.05 *** 0.01) se r2 br aic 

generate imp_year=importer*year
generate exp_year=exporter*year
eststo: flex products_bytype agree_pta member_eu_joint high_int_ex low_int_ex high_any_dig_ex low_any_dig_ex  bandwidth_index log_distance contiguity common_language colony_ever  i.imp_year i.exp_year if goods_svcs=="services" & year>2004, ul(17)

esttab using "flex_svcs_results.csv", star(* 0.10 ** 0.05 *** 0.01) se r2 br aic 

****************3.3.2 Individual Digital Trade Provisions*********************
use "aggregate_trade_data.dta", clear 
generate colony_ever = colony_of_destination_ever+colony_of_origin_ever
generate log_distance=log(distance)

*bandwidth from ITU reported in kbits/s Convert to mbit/s
replace minimum_bandwidth=minimum_bandwidth/1000
generate bandwidth_cat=0
replace bandwidth_cat=1 if minimum_bandwidth>=1 & minimum_bandwidth<3
replace bandwidth_cat=2 if minimum_bandwidth>=3
tab bandwidth_cat, gen(bandwidth_cat_)

egen max_bandwidth=max(minimum_bandwidth)
generate bandwidth_index = minimum_bandwidth/max_bandwidth

decode importer, generate(imp_iso3)
decode exporter, generate(exp_iso3)

*Generate variable that indicates high income importer (both high and upper middle income)
generate high_income_exp=0

replace high_income_exp=1 if exp_iso3=="AND" | exp_iso3=="ATG" | exp_iso3=="ABW" | exp_iso3=="AUS" | exp_iso3=="AUT" | exp_iso3=="BHS" | exp_iso3=="BHR" | exp_iso3=="BRB" | exp_iso3=="BEL" | exp_iso3=="BMU" | exp_iso3=="VGB" | exp_iso3=="BRN" | exp_iso3=="CAN" | exp_iso3=="CYM" | exp_iso3=="CHL" | exp_iso3=="HRV" | exp_iso3=="CUW" | exp_iso3=="CYP" | exp_iso3=="CZE" | exp_iso3=="DNK" | exp_iso3=="EST" | exp_iso3=="FRO" | exp_iso3=="FIN" | exp_iso3=="FRA" | exp_iso3=="PYF" | exp_iso3=="DEU" | exp_iso3=="GIB" | exp_iso3=="GRC" | exp_iso3=="GRL" | exp_iso3=="GUM" | exp_iso3=="HKG" | exp_iso3=="HUN" | exp_iso3=="ISL" | exp_iso3=="IRL" | exp_iso3=="IMN" | exp_iso3=="ISR" | exp_iso3=="ITA" | exp_iso3=="JPN" | exp_iso3=="KOR" | exp_iso3=="KWT" | exp_iso3=="LVA" | exp_iso3=="LIE" | exp_iso3=="LTU" | exp_iso3=="LUX" | exp_iso3=="MAC" | exp_iso3=="MLT" | exp_iso3=="MCO" | exp_iso3=="NRU" | exp_iso3=="NLD" | exp_iso3=="NCL" | exp_iso3=="NZL" | exp_iso3=="MNP" | exp_iso3=="NOR" | exp_iso3=="OMN" | exp_iso3=="PLW" | exp_iso3=="POL" | exp_iso3=="PRT" | exp_iso3=="PRI" | exp_iso3=="QAT" | exp_iso3=="SMR" | exp_iso3=="SAU" | exp_iso3=="SYC" | exp_iso3=="SGP" | exp_iso3=="SXM" | exp_iso3=="SVK" | exp_iso3=="SVN" | exp_iso3=="ESP" | exp_iso3=="KNA" | exp_iso3=="MAF" | exp_iso3=="SWE" | exp_iso3=="CHE" | exp_iso3=="TWN" | exp_iso3=="TTO" | exp_iso3=="TCA" | exp_iso3=="ARE" | exp_iso3=="GBR" | exp_iso3=="USA" | exp_iso3=="URY" | exp_iso3=="VIR"  | exp_iso3=="ROU" | exp_iso3=="PAN"
*upper middle income
replace high_income_exp=1 if exp_iso3=="ALB" | exp_iso3=="FJI" | exp_iso3=="NAM" | exp_iso3=="ASM" | exp_iso3=="GAB" | exp_iso3=="MKD" | exp_iso3=="ARG" | exp_iso3=="GEO" | exp_iso3=="PLW" | exp_iso3=="ARM" | exp_iso3=="GRD" | exp_iso3=="PRY" | exp_iso3=="AZE" | exp_iso3=="GTM" | exp_iso3=="PER" | exp_iso3=="BLR" | exp_iso3=="GUY" | exp_iso3=="RUS" | exp_iso3=="BLZ" | exp_iso3=="IRQ" | exp_iso3=="SRB" | exp_iso3=="BIH" | exp_iso3=="JAM" | exp_iso3=="ZAF" | exp_iso3=="BWA" | exp_iso3=="JOR" | exp_iso3=="LCA" | exp_iso3=="BRA" | exp_iso3=="KAZ" | exp_iso3=="VCT" | exp_iso3=="BLG" | exp_iso3=="KSV" | exp_iso3=="SUR" | exp_iso3=="CHN" | exp_iso3=="LBY" | exp_iso3=="THA" | exp_iso3=="COL" | exp_iso3=="MYS" | exp_iso3=="TON" | exp_iso3=="CRI" | exp_iso3=="MDV" | exp_iso3=="TUR" | exp_iso3=="CUB" | exp_iso3=="MHL" | exp_iso3=="TKM" | exp_iso3=="DMA" | exp_iso3=="MUS" | exp_iso3=="TUV" | exp_iso3=="DOM" | exp_iso3=="MEX" | exp_iso3=="GNQ" | exp_iso3=="MDA" | exp_iso3=="ECU" | exp_iso3=="MNE"  


generate low_inc_exp=0
replace low_inc_exp=1 if high_income_exp==0

gen high_int_ex=high_income_exp*internet_use_pair
gen low_int_ex = low_inc_exp*internet_use_pair


foreach v of var tech_neutrality- free_data_flow{
	replace `v' = 1 if `v'>0
}

generate any_digital = 1 if free_data_flow==1 | tech_neutrality==1 | ec_duty_prohibit==1 | electronic_sig==1 | data_protection==1 | cybersecurity==1 | goods_big_data==1
replace any_digital = 0 if any_digital==.

generate provision_count=free_data_flow+tech_neutrality+ec_duty_prohibit+electronic_sig+data_protection+cybersecurity+goods_big_data
replace provision_count=0 if provision_count==.
generate provision_index = provision_count/7

gen high_digital_ex=high_income_exp*provision_index
gen low_digital_ex=low_inc_exp*provision_index
gen high_any_dig_ex=high_income_exp*any_digital
gen low_any_dig_ex=low_inc_exp*any_digital
eststo clear
eststo: ppmlhdfe  trade    free_data_flow internet_use_pair bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016, absorb(i.importer#i.year i.exporter#i.year i.importer#i.exporter) cluster(i.importer#i.exporter)

eststo: ppmlhdfe  trade  tech_neutrality internet_use_pair bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016, absorb(i.importer#i.year i.exporter#i.year i.importer#i.exporter) cluster(i.importer#i.exporter)

eststo: ppmlhdfe  trade  ec_duty_prohibit internet_use_pair bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016, absorb(i.importer#i.year i.exporter#i.year i.importer#i.exporter) cluster(i.importer#i.exporter)

eststo: ppmlhdfe  trade  electronic_sig internet_use_pair bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016, absorb(i.importer#i.year i.exporter#i.year i.importer#i.exporter) cluster(i.importer#i.exporter)

eststo: ppmlhdfe  trade   data_protection internet_use_pair bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016, absorb(i.importer#i.year i.exporter#i.year i.importer#i.exporter) cluster(i.importer#i.exporter)

eststo: ppmlhdfe  trade  cybersecurity internet_use_pair bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016, absorb(i.importer#i.year i.exporter#i.year i.importer#i.exporter) cluster(i.importer#i.exporter)

eststo: ppmlhdfe  trade  goods_big_data internet_use_pair bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016, absorb(i.importer#i.year i.exporter#i.year i.importer#i.exporter) cluster(i.importer#i.exporter)

eststo: ppmlhdfe  trade free_data_flow tech_neutrality ec_duty_prohibit electronic_sig data_protection cybersecurity goods_big_data  internet_use_pair bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016, absorb(i.importer#i.year i.exporter#i.year i.importer#i.exporter) cluster(i.importer#i.exporter)


esttab using "indiv_provisions_results.csv", star(* 0.10 ** 0.05 *** 0.01) se r2 br aic scalars(r2_p) replace 

********************3.3.5 Technical Robustness Checks***************************

*bias correction PPML-Weidner and Zylkin (2021)

eststo clear

eststo: ppmlhdfe trade high_int_ex low_int_ex high_any_dig_ex low_any_dig_ex  bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016, absorb(i.importer#i.year i.exporter#i.year i.importer#i.exporter) cluster(i.importer#i.exporter) d

predict lambda_all
matrix beta_all = e(b)

eststo: ppml_fe_bias  trade high_int_ex low_int_ex high_any_dig_ex low_any_dig_ex  bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016, lambda(lambda_all) i(exporter) j(importer) t(year) beta(beta_all) 

esttab using "biascorrect_results.csv", star(* 0.10 ** 0.05 *** 0.01) se r2 br aic scalars(r2_p) replace

*skip years
eststo clear
generate three_year=0
replace three_year=1 if year==2000 | year==2003 | year==2006 | year==2009 | year==2012 | year==2015

eststo: ppmlhdfe trade high_int_ex low_int_ex high_any_dig_ex low_any_dig_ex  bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016 if three_year==1, absorb(i.importer#i.year i.exporter#i.year i.importer#i.exporter) cluster(i.importer#i.exporter)

generate five_year=0 
replace five_year=1 if year==2000 | year==2005 | year==2010 | year==2015

eststo: ppmlhdfe trade high_int_ex low_int_ex high_any_dig_ex low_any_dig_ex  bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016 if five_year==1, absorb(i.importer#i.year i.exporter#i.year i.importer#i.exporter) cluster(i.importer#i.exporter)

esttab using "yeargap_results.csv", star(* 0.10 ** 0.05 *** 0.01) se r2 br aic scalars(r2_p) replace 


*******************************3.3.3 Industry-Specific Results************************
use "digital_trade_sector.dta", clear




replace minimum_bandwidth=minimum_bandwidth/1000
generate bandwidth_cat=0
replace bandwidth_cat=1 if minimum_bandwidth>=1 & minimum_bandwidth<3
replace bandwidth_cat=2 if minimum_bandwidth>=3
tab bandwidth_cat, gen(bandwidth_cat_)


egen max_bandwidth=max(minimum_bandwidth)
generate bandwidth_index = minimum_bandwidth/max_bandwidth

decode importer, generate(imp_iso3)
decode exporter, generate(exp_iso3)

*Generate variable that indicates high income importer (both high and upper middle income)
generate high_income_exp=0

replace high_income_exp=1 if exp_iso3=="AND" | exp_iso3=="ATG" | exp_iso3=="ABW" | exp_iso3=="AUS" | exp_iso3=="AUT" | exp_iso3=="BHS" | exp_iso3=="BHR" | exp_iso3=="BRB" | exp_iso3=="BEL" | exp_iso3=="BMU" | exp_iso3=="VGB" | exp_iso3=="BRN" | exp_iso3=="CAN" | exp_iso3=="CYM" | exp_iso3=="CHL" | exp_iso3=="HRV" | exp_iso3=="CUW" | exp_iso3=="CYP" | exp_iso3=="CZE" | exp_iso3=="DNK" | exp_iso3=="EST" | exp_iso3=="FRO" | exp_iso3=="FIN" | exp_iso3=="FRA" | exp_iso3=="PYF" | exp_iso3=="DEU" | exp_iso3=="GIB" | exp_iso3=="GRC" | exp_iso3=="GRL" | exp_iso3=="GUM" | exp_iso3=="HKG" | exp_iso3=="HUN" | exp_iso3=="ISL" | exp_iso3=="IRL" | exp_iso3=="IMN" | exp_iso3=="ISR" | exp_iso3=="ITA" | exp_iso3=="JPN" | exp_iso3=="KOR" | exp_iso3=="KWT" | exp_iso3=="LVA" | exp_iso3=="LIE" | exp_iso3=="LTU" | exp_iso3=="LUX" | exp_iso3=="MAC" | exp_iso3=="MLT" | exp_iso3=="MCO" | exp_iso3=="NRU" | exp_iso3=="NLD" | exp_iso3=="NCL" | exp_iso3=="NZL" | exp_iso3=="MNP" | exp_iso3=="NOR" | exp_iso3=="OMN" | exp_iso3=="PLW" | exp_iso3=="POL" | exp_iso3=="PRT" | exp_iso3=="PRI" | exp_iso3=="QAT" | exp_iso3=="SMR" | exp_iso3=="SAU" | exp_iso3=="SYC" | exp_iso3=="SGP" | exp_iso3=="SXM" | exp_iso3=="SVK" | exp_iso3=="SVN" | exp_iso3=="ESP" | exp_iso3=="KNA" | exp_iso3=="MAF" | exp_iso3=="SWE" | exp_iso3=="CHE" | exp_iso3=="TWN" | exp_iso3=="TTO" | exp_iso3=="TCA" | exp_iso3=="ARE" | exp_iso3=="GBR" | exp_iso3=="USA" | exp_iso3=="URY" | exp_iso3=="VIR"  | exp_iso3=="ROU" | exp_iso3=="PAN"
*upper middle income
replace high_income_exp=1 if exp_iso3=="ALB" | exp_iso3=="FJI" | exp_iso3=="NAM" | exp_iso3=="ASM" | exp_iso3=="GAB" | exp_iso3=="MKD" | exp_iso3=="ARG" | exp_iso3=="GEO" | exp_iso3=="PLW" | exp_iso3=="ARM" | exp_iso3=="GRD" | exp_iso3=="PRY" | exp_iso3=="AZE" | exp_iso3=="GTM" | exp_iso3=="PER" | exp_iso3=="BLR" | exp_iso3=="GUY" | exp_iso3=="RUS" | exp_iso3=="BLZ" | exp_iso3=="IRQ" | exp_iso3=="SRB" | exp_iso3=="BIH" | exp_iso3=="JAM" | exp_iso3=="ZAF" | exp_iso3=="BWA" | exp_iso3=="JOR" | exp_iso3=="LCA" | exp_iso3=="BRA" | exp_iso3=="KAZ" | exp_iso3=="VCT" | exp_iso3=="BLG" | exp_iso3=="KSV" | exp_iso3=="SUR" | exp_iso3=="CHN" | exp_iso3=="LBY" | exp_iso3=="THA" | exp_iso3=="COL" | exp_iso3=="MYS" | exp_iso3=="TON" | exp_iso3=="CRI" | exp_iso3=="MDV" | exp_iso3=="TUR" | exp_iso3=="CUB" | exp_iso3=="MHL" | exp_iso3=="TKM" | exp_iso3=="DMA" | exp_iso3=="MUS" | exp_iso3=="TUV" | exp_iso3=="DOM" | exp_iso3=="MEX" | exp_iso3=="GNQ" | exp_iso3=="MDA" | exp_iso3=="ECU" | exp_iso3=="MNE"  


generate low_inc_exp=0
replace low_inc_exp=1 if high_income_exp==0

gen high_int_ex=high_income_exp*internet_use_pair
gen low_int_ex = low_inc_exp*internet_use_pair


foreach v of var tech_neutrality- free_data_flow{
	replace `v' = 1 if `v'>0
}

generate any_digital = 1 if free_data_flow==1 | tech_neutrality==1 | ec_duty_prohibit==1 | electronic_sig==1 | data_protection==1 | cybersecurity==1 | goods_big_data==1
replace any_digital = 0 if any_digital==.

generate provision_count=free_data_flow+tech_neutrality+ec_duty_prohibit+electronic_sig+data_protection+cybersecurity+goods_big_data
replace provision_count=0 if provision_count==.
generate provision_index = provision_count/7

gen high_any_dig_ex=high_income_exp*any_digital
gen low_any_dig_ex=low_inc_exp*any_digital
gen high_digital_ex=high_income_exp*provision_index
gen low_digital_ex=low_inc_exp*provision_index

*Table A4
bysort sector: sum trade


encode sector, generate(sector2)
eststo clear
forvalues s=1/12 {

eststo: ppmlhdfe  trade high_int_ex low_int_ex high_any_dig_ex low_any_dig_ex bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016 if sector2==`s', absorb(i.importer#i.year i.exporter#i.year i.importer#i.exporter) cluster(i.importer#i.exporter)
}

esttab using "sub_sectors_results.csv", star(* 0.10 ** 0.05 *** 0.01) se r2 br aic scalars(r2_p) replace

**************3.3.4  Alternative Groupings of Countries and Sectors*****************
use "goods_services_trade_data.dta", clear 

replace minimum_bandwidth=minimum_bandwidth/1000
generate bandwidth_cat=0
replace bandwidth_cat=1 if minimum_bandwidth>=1 & minimum_bandwidth<3
replace bandwidth_cat=2 if minimum_bandwidth>=3
tab bandwidth_cat, gen(bandwidth_cat_)

egen max_bandwidth=max(minimum_bandwidth)
generate bandwidth_index = minimum_bandwidth/max_bandwidth

decode importer, generate(imp_iso3)
decode exporter, generate(exp_iso3)

merge m:1 imp_iso3 using "stri.dta"

drop _m

merge m:1 imp_iso3 using "kee_tri.dta"

generate low_stri=0
replace low_stri=1 if high_stri==0

gen high_stri_int=high_stri*internet_use_pair
gen low_stri_int = low_stri*internet_use_pair

generate low_tariff_ntms=0
replace low_tariff_ntms=1 if high_tariff_ntms==0

gen high_tariff_int=high_tariff_ntms*internet_use_pair
gen low_tariff_int = low_tariff_ntms*internet_use_pair

foreach v of var tech_neutrality- free_data_flow{
	replace `v' = 1 if `v'>0
}

generate any_digital = 1 if free_data_flow==1 | tech_neutrality==1 | ec_duty_prohibit==1 | electronic_sig==1 | data_protection==1 | cybersecurity==1 | goods_big_data==1
replace any_digital = 0 if any_digital==.

generate provision_count=free_data_flow+tech_neutrality+ec_duty_prohibit+electronic_sig+data_protection+cybersecurity+goods_big_data
replace provision_count=0 if provision_count==.
generate provision_index = provision_count/7

gen high_stri_digital=high_stri*provision_index
gen low_stri_digital=low_stri*provision_index
gen high_stri_any_dig=high_stri*any_digital
gen low_stri_any_dig=low_stri*any_digital

gen high_tariff_digital=high_tariff_ntms*provision_index
gen low_tariff_digital=low_tariff_ntms*provision_index
gen high_tariff_any_dig=high_tariff_ntms*any_digital
gen low_tariff_any_dig=low_tariff_ntms*any_digital

eststo clear

eststo: ppmlhdfe trade high_stri_int low_stri_int high_stri_any_dig low_stri_any_dig bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016 if stri!=. & goods_svcs=="services", absorb(i.importer#i.year i.exporter#i.year i.importer#i.exporter) cluster(i.importer#i.exporter) 

eststo: ppmlhdfe trade high_stri_int low_stri_int high_stri_digital low_stri_digital bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016 if stri!=. & goods_svcs=="services", absorb(i.importer#i.year i.exporter#i.year i.importer#i.exporter) cluster(i.importer#i.exporter)

eststo: ppmlhdfe trade high_tariff_int low_tariff_int high_tariff_any_dig low_tariff_any_dig bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016 if OTRI!=. & goods_svcs=="goods", absorb(i.importer#i.year i.exporter#i.year i.importer#i.exporter) cluster(i.importer#i.exporter) 

eststo: ppmlhdfe trade high_tariff_int low_tariff_int high_tariff_digital low_tariff_digital bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016 if OTRI!=. & goods_svcs=="goods", absorb(i.importer#i.year i.exporter#i.year i.importer#i.exporter) cluster(i.importer#i.exporter)

esttab using "traderestrictions_split_results.csv", star(* 0.10 ** 0.05 *** 0.01) se r2 scalars(r2_p) br aic  replace

*High-low IT use
use "IT_use_split.dta", clear

replace minimum_bandwidth=minimum_bandwidth/1000
generate bandwidth_cat=0
replace bandwidth_cat=1 if minimum_bandwidth>=1 & minimum_bandwidth<3
replace bandwidth_cat=2 if minimum_bandwidth>=3
tab bandwidth_cat, gen(bandwidth_cat_)

egen max_bandwidth=max(minimum_bandwidth)
generate bandwidth_index = minimum_bandwidth/max_bandwidth

decode importer, generate(imp_iso3)
decode exporter, generate(exp_iso3)

*Generate variable that indicates high income importer (both high and upper middle income)
generate high_income_exp=0

replace high_income_exp=1 if exp_iso3=="AND" | exp_iso3=="ATG" | exp_iso3=="ABW" | exp_iso3=="AUS" | exp_iso3=="AUT" | exp_iso3=="BHS" | exp_iso3=="BHR" | exp_iso3=="BRB" | exp_iso3=="BEL" | exp_iso3=="BMU" | exp_iso3=="VGB" | exp_iso3=="BRN" | exp_iso3=="CAN" | exp_iso3=="CYM" | exp_iso3=="CHL" | exp_iso3=="HRV" | exp_iso3=="CUW" | exp_iso3=="CYP" | exp_iso3=="CZE" | exp_iso3=="DNK" | exp_iso3=="EST" | exp_iso3=="FRO" | exp_iso3=="FIN" | exp_iso3=="FRA" | exp_iso3=="PYF" | exp_iso3=="DEU" | exp_iso3=="GIB" | exp_iso3=="GRC" | exp_iso3=="GRL" | exp_iso3=="GUM" | exp_iso3=="HKG" | exp_iso3=="HUN" | exp_iso3=="ISL" | exp_iso3=="IRL" | exp_iso3=="IMN" | exp_iso3=="ISR" | exp_iso3=="ITA" | exp_iso3=="JPN" | exp_iso3=="KOR" | exp_iso3=="KWT" | exp_iso3=="LVA" | exp_iso3=="LIE" | exp_iso3=="LTU" | exp_iso3=="LUX" | exp_iso3=="MAC" | exp_iso3=="MLT" | exp_iso3=="MCO" | exp_iso3=="NRU" | exp_iso3=="NLD" | exp_iso3=="NCL" | exp_iso3=="NZL" | exp_iso3=="MNP" | exp_iso3=="NOR" | exp_iso3=="OMN" | exp_iso3=="PLW" | exp_iso3=="POL" | exp_iso3=="PRT" | exp_iso3=="PRI" | exp_iso3=="QAT" | exp_iso3=="SMR" | exp_iso3=="SAU" | exp_iso3=="SYC" | exp_iso3=="SGP" | exp_iso3=="SXM" | exp_iso3=="SVK" | exp_iso3=="SVN" | exp_iso3=="ESP" | exp_iso3=="KNA" | exp_iso3=="MAF" | exp_iso3=="SWE" | exp_iso3=="CHE" | exp_iso3=="TWN" | exp_iso3=="TTO" | exp_iso3=="TCA" | exp_iso3=="ARE" | exp_iso3=="GBR" | exp_iso3=="USA" | exp_iso3=="URY" | exp_iso3=="VIR"  | exp_iso3=="ROU" | exp_iso3=="PAN"
*upper middle income
replace high_income_exp=1 if exp_iso3=="ALB" | exp_iso3=="FJI" | exp_iso3=="NAM" | exp_iso3=="ASM" | exp_iso3=="GAB" | exp_iso3=="MKD" | exp_iso3=="ARG" | exp_iso3=="GEO" | exp_iso3=="PLW" | exp_iso3=="ARM" | exp_iso3=="GRD" | exp_iso3=="PRY" | exp_iso3=="AZE" | exp_iso3=="GTM" | exp_iso3=="PER" | exp_iso3=="BLR" | exp_iso3=="GUY" | exp_iso3=="RUS" | exp_iso3=="BLZ" | exp_iso3=="IRQ" | exp_iso3=="SRB" | exp_iso3=="BIH" | exp_iso3=="JAM" | exp_iso3=="ZAF" | exp_iso3=="BWA" | exp_iso3=="JOR" | exp_iso3=="LCA" | exp_iso3=="BRA" | exp_iso3=="KAZ" | exp_iso3=="VCT" | exp_iso3=="BLG" | exp_iso3=="KSV" | exp_iso3=="SUR" | exp_iso3=="CHN" | exp_iso3=="LBY" | exp_iso3=="THA" | exp_iso3=="COL" | exp_iso3=="MYS" | exp_iso3=="TON" | exp_iso3=="CRI" | exp_iso3=="MDV" | exp_iso3=="TUR" | exp_iso3=="CUB" | exp_iso3=="MHL" | exp_iso3=="TKM" | exp_iso3=="DMA" | exp_iso3=="MUS" | exp_iso3=="TUV" | exp_iso3=="DOM" | exp_iso3=="MEX" | exp_iso3=="GNQ" | exp_iso3=="MDA" | exp_iso3=="ECU" | exp_iso3=="MNE"  


generate low_inc_exp=0
replace low_inc_exp=1 if high_income_exp==0

gen high_int_ex=high_income_exp*internet_use_pair
gen low_int_ex = low_inc_exp*internet_use_pair



*Dummy for presence, not for magnitude
foreach v of var tech_neutrality- free_data_flow{
	replace `v' = 1 if `v'>0
}

*Table A2
pwcorr free_data_flow tech_neutrality ec_duty_prohibit electronic_sig data_protection cybersecurity goods_big_data, star(0.05)

generate any_digital = 1 if free_data_flow==1 | tech_neutrality==1 | ec_duty_prohibit==1 | electronic_sig==1 | data_protection==1 | cybersecurity==1 | goods_big_data==1
replace any_digital = 0 if any_digital==.

generate provision_count=free_data_flow+tech_neutrality+ec_duty_prohibit+electronic_sig+data_protection+cybersecurity+goods_big_data
replace provision_count=0 if provision_count==.
generate provision_index = provision_count/7

gen high_digital_ex=high_income_exp*provision_index
gen low_digital_ex=low_inc_exp*provision_index
gen high_any_dig_ex=high_income_exp*any_digital
gen low_any_dig_ex=low_inc_exp*any_digital

eststo clear

forvalues s=0/1 {
eststo: ppmlhdfe  trade high_int_ex low_int_ex high_digital_ex low_digital_ex bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016 if high_it==`s', absorb(i.importer#i.year i.exporter#i.year i.importer#i.exporter) cluster(i.importer#i.exporter)

eststo: ppmlhdfe  trade high_int_ex low_int_ex high_any_dig_ex low_any_dig_ex  bandwidth_index agree_pta member_eu_joint foreign_2001-foreign_2016 if high_it==`s', absorb(i.importer#i.year i.exporter#i.year i.importer#i.exporter) cluster(i.importer#i.exporter)
}
esttab using "it_use_split_results.csv", star(* 0.10 ** 0.05 *** 0.01) se r2 br aic scalars(r2_p) replace 
